package com.whjfxx.terminal.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whjfxx.terminal.biz.mapper.PoAppMapper;
import com.whjfxx.terminal.biz.mapper.PoFuncMapper;
import com.whjfxx.terminal.biz.mapper.SysOrganizationMapper;
import com.whjfxx.terminal.biz.param.AppPageParam;
import com.whjfxx.terminal.biz.param.AppParam;
import com.whjfxx.terminal.biz.param.SearchParam;
import com.whjfxx.terminal.biz.service.PoAppService;
import com.whjfxx.terminal.biz.service.PoFuncService;
import com.whjfxx.terminal.biz.service.SysOrganizationService;
import com.whjfxx.terminal.biz.vo.AppVo;
import com.whjfxx.terminal.entity.PoApp;
import com.whjfxx.terminal.entity.PoFunc;
import com.whjfxx.terminal.entity.SysOrganization;
import com.whjfxx.terminal.exceptions.BizException;
import com.whjfxx.terminal.utils.BeanUtils;
import com.whjfxx.terminal.utils.PageUtils;
import com.whjfxx.terminal.utils.QueryUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Created by b.c on 2022/6/7 16:28
 */
@Service
public class PoAppServiceImpl extends ServiceImpl<PoAppMapper, PoApp> implements PoAppService {

    @Autowired
    SysOrganizationService sysOrganizationService;

    @Autowired
    PoFuncMapper poFuncMapper;

    @Override
    public Page<AppVo> pageList(SearchParam<AppPageParam> searchParam) {
        LambdaQueryWrapper<PoApp> wrapper = new LambdaQueryWrapper();
        QueryUtil.search(searchParam, AppPageParam.class, (pageParam) -> {
            if (StringUtils.isNotBlank(pageParam.getAppName())) {
                wrapper.like(PoApp::getAppName, pageParam.getAppName());
            }
            if (StringUtils.isNotBlank(pageParam.getCustName())) {
                wrapper.like(PoApp::getCustName, pageParam.getCustName());
            }
            if (StringUtils.isNotBlank(pageParam.getCustMobile())) {
                wrapper.like(PoApp::getCustMobile, pageParam.getCustMobile());
            }
        });
        this.page(searchParam, wrapper);

        Page<PoApp> page = PageUtils.buildPage(searchParam);


        List<Long> organizationIds = page.getRecords().stream().map(PoApp::getOrganizationId).filter(Objects::nonNull).collect(Collectors.toList());

        List<SysOrganization> organizations = sysOrganizationService.getByIds(organizationIds);
        Map<Long, SysOrganization> organizationMap = organizations.stream().collect(Collectors.toMap(SysOrganization::getId, Function.identity()));

        Page<AppVo> pageVo = PageUtils.convert(page, AppVo.class, (src, target) -> {
            SysOrganization organization = organizationMap.get(src.getOrganizationId());
            if (Objects.nonNull(organization)) {
                target.setOrganizationName(organization.getOrganizationName());
            }
        });
        return pageVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertSave(AppParam param) {
        // 校验应用名不能重复
        PoApp poApp = this.baseMapper.selectByAppName(param.getAppName());
        if (Objects.nonNull(poApp)) {
            throw BizException.build("应用名已经存在");
        }
        poApp = new PoApp();
        BeanUtils.copyProperties(param, poApp);
        poApp.setCreateTime(LocalDateTime.now());
        poApp.setUpdateTime(LocalDateTime.now());
        this.save(poApp);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSave(AppParam param) {
        // 校验应用名不能重复
        PoApp poApp = this.baseMapper.selectById(param.getId());
        if (Objects.isNull(poApp)) {
            throw BizException.build("应用信息不存在");
        }

        BeanUtils.copyProperties(param, poApp);
        poApp.setAppName(null);
        poApp.setUpdateTime(LocalDateTime.now());
        this.updateById(poApp);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        if (Objects.isNull(id)) {
            return;
        }
        // 查询应用是否存在 功能
        LambdaQueryWrapper<PoFunc> wrapper = new LambdaQueryWrapper();
        wrapper.eq(PoFunc::getAppId, id);
        int i = poFuncMapper.selectCount(wrapper);
        if (i > 0) {
            throw BizException.build("应用下存在的功能信息，无法删除");
        }

        this.baseMapper.logicDelete(id, LocalDateTime.now());
    }
}
